package com.study.rabbitmq.config;





import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.URI;


@Configuration
public class RabbitConfig {

    //ConnectionFactory
    @Bean
    public ConnectionFactory getConnectionFactory(){
        ConnectionFactory connectionFactory = new CachingConnectionFactory(URI.create("amqp://root:123456@192.168.181.131:5672/%2f"));
        return connectionFactory;
    }

    //RabbitTemplate
   @Bean
   @Autowired
    public RabbitTemplate getRabbitTemplate(ConnectionFactory connectionFactory){

       RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);

       return rabbitTemplate;
   }

   //rabbitAdmin
   @Bean
   @Autowired
    public RabbitAdmin getRabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        return rabbitAdmin;
    }

    //queue
    @Bean
    public Queue getQueue(){
        Queue queue = QueueBuilder.nonDurable("queue.anno").build();
        return queue;

    }

    //Exchage
    @Bean
    public Exchange getExchange(){
        Exchange exchange = new FanoutExchange("exchange.anno",false,false,null);
        return exchange;
    }


    //Binding
    @Bean
    @Autowired
    public Binding getBinding(Queue queue,Exchange exchange){
        Binding binding = BindingBuilder.bind(queue).to(exchange).with("routingkey.annotation").noargs();
        return binding;
    }

}
